﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_View_F5YoyakuSupply : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        private DataTable supplyTable;

        public frm_View_F5YoyakuSupply()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.supplyTable = new DataTable();
            this.supplyTable.Columns.Add("OrderNo", typeof(string));
            this.supplyTable.Columns.Add("PartNo", typeof(string));
            this.supplyTable.Columns.Add("TotalQuantity", typeof(float));
            this.supplyTable.Columns.Add("DeliveryDate", typeof(string));
            this.supplyTable.Columns.Add("ArrivalDate", typeof(string));
        }

        /////////////////////////////////////////
        /// Load Data
        ////////////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT [ORDERNO], [PARTNO], [TOTALQUANTITY],"
                    + " CASE WHEN [DELIVERYDATE]='0' THEN CONVERT(VARCHAR(10), DATEADD(YEAR, 1, GETDATE()), 112) ELSE [DELIVERYDATE] END AS [DELIVERYDATE],"
                    + " CASE WHEN [ARRIVALDATE]='00000000' THEN CONVERT(VARCHAR(10), DATEADD(YEAR, 1, GETDATE()), 112) ELSE [ARRIVALDATE] END AS [ARRIVALDATE]"
                    + " FROM [VF5TotalHave]"
                    + " WHERE [PARTNO] COLLATE Vietnamese_CI_AS IN (SELECT PartNo FROM [231_F5YoyakuPlanKosei])";
                sql_cmd.CommandTimeout = 30000;
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    DataRow new_row = this.supplyTable.NewRow();
                    new_row["OrderNo"] = dataReader["ORDERNO"].ToString().Trim();
                    new_row["PartNo"] = dataReader["PARTNO"].ToString().Trim();
                    float quantity = 0;
                    float.TryParse(dataReader["TOTALQUANTITY"].ToString().Trim(), out quantity);
                    new_row["TotalQuantity"] = quantity;
                    new_row["DeliveryDate"] = dataReader["DELIVERYDATE"].ToString().Trim();
                    new_row["ArrivalDate"] = dataReader["ARRIVALDATE"].ToString().Trim();
                    this.supplyTable.Rows.Add(new_row);
                }
                dataReader.Close();
                sql_cmd.Dispose();
                myConnection.Close();

                if (this.supplyTable.Rows.Count > 0)
                {
                    gridF5SupplyList.DataSource = this.supplyTable;
                    
                    F5SupplyList.Columns[0].Caption = "Order No.";
                    F5SupplyList.Columns[1].Caption = "Part No.";
                    F5SupplyList.Columns[2].Caption = "Total Quantity";
                    F5SupplyList.Columns[3].Caption = "Delivery Date";
                    F5SupplyList.Columns[4].Caption = "Arrival Date";                                        
                    F5SupplyList.BestFitColumns();
                }
                else
                {
                    ret = false;
                }
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                ret = false;
            }           

            return ret;
        }
    }
}
